package Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet("/DelService")
public class DelService extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String time = request.getParameter("time");
        String location = request.getParameter("locate");
        String id = request.getParameter("id");
        PrintWriter out = response.getWriter();
//        out.print(fid[0]);

        Connection con=null;
        PreparedStatement pstmt=null;
        String sql;
        ResultSet rs;
        try{  //加载JDBC-MySQL8.0连接器:
            Class.forName("com.mysql.cj.jdbc.Driver");
        }
        catch(Exception e){
            out.println("加载失败");
        }
        String url = "jdbc:mysql://localhost:3306/yundonghui?"+
                "useSSL=false&serverTimezone=CST&characterEncoding=utf-8";
        String user ="root";
        String password ="123456";
        try {
            con = DriverManager.getConnection(url,user,password);

            try {
                sql="delete from zhiyuanzhe where id=? and ztime=? and location=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1,id);
                pstmt.setString(2,time);
                pstmt.setString(3,location);
                pstmt.executeUpdate();
                response.getWriter().write("<script language='javascript'>alert('删除成功！');" +
                        "window.location.href='/LookService';</script>");
            }catch (SQLException e){
                response.getWriter().write("<script language='javascript'>alert('删除失败！');" +
                        "window.location.href='/zhiyuanzhe/myService.jsp';</script>");
            }

            con.close();
        } catch (SQLException e) {
            out.println("连接失败");
        }//连接数据库。
    }
}
